package com.gem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gem.entity.UserAddress;
import com.gem.mapper.UserAddressMapper;
import com.gem.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘聪
 * Created  on 2021/3/19
 */
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService {
    @Autowired
    private UserAddressMapper addressMapper;
    /*
        @Override
        public List<UserAddress> addressList(Integer id) {
            return addressMapper.addressList(id);
        }*/

    @Override
    public List<UserAddress> dafultAddress(Integer userId, Integer addressId) {
        QueryWrapper wrapper = new QueryWrapper();
        Map<String, Integer> map = new HashMap<>();
        map.put("user_id", userId);
        map.put("is_default", 0);
        wrapper.allEq(map);
        UserAddress oldAddress = addressMapper.selectOne(wrapper);
        if (!addressId.equals(oldAddress.getId())) {
            UserAddress newAddress = addressMapper.selectById(addressId);
            oldAddress.setIsDefault(1);
            newAddress.setIsDefault(0);
            List<UserAddress> addressList = new ArrayList<>();
            addressList.add(oldAddress);
            addressList.add(newAddress);
            return addressList;
        } else {
            List<UserAddress> addressList = new ArrayList<>();
            addressList.add(oldAddress);
            return addressList;
        }
    }
}
